package com.apes.fn.crm.ReturnVisit.service;

import com.apes.fn.crm.ReturnVisit.model.VisitCondition;
import com.apes.fn.crm.ReturnVisit.repository.VisitConditionRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.party.model.AccessProvider;
import com.apes.scm.masterdata.party.repository.AccessProviderRepository;
import com.apes.scm.rbac.model.Post;
import com.apes.scm.rbac.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("visitConditionService")
public class VisitConditionService extends DomainService {

    @Autowired
    VisitConditionRepository visitConditionRepository;
    @Autowired
    PostRepository postRepository;
    @Autowired
    AccessProviderRepository accessProviderRepository;
    /***
     * 创建
     */
    public VisitCondition create(SimpleRequest request) {
        VisitCondition visitCondition = request.getO(VisitCondition.class);
        Post post = postRepository.findOne(request.getPostId());
        AccessProvider accessProvider = accessProviderRepository.findByCompany(post.getCompany().getId());
        visitCondition.setAccessProvider(accessProvider);

        // 限制一个服务类型只能有一行数据
        if(visitConditionRepository.findOne(JpaDsl.toCriteriaByEq(
                "serveType.id", visitCondition.getServeType().getId()
        )).orElse(null) != null) throw new RuntimeException("当前服务类型已定义条件！");

        return visitConditionRepository.saveAndFlush(visitCondition);
    }

    /***
     * 修改
     */
    public VisitCondition update(SimpleRequest request) {
        isValid(request, visitConditionRepository);
        VisitCondition visitCondition = request.getO(VisitCondition.class);
        Post post = postRepository.findOne(request.getPostId());
        AccessProvider accessProvider = accessProviderRepository.findByCompany(post.getCompany().getId());
        visitCondition.setAccessProvider(accessProvider);

        // 限制一个服务类型只能有一行数据
        if(visitConditionRepository.findOne(JpaDsl.toCriteria(
                "serveType.id", "EQ", visitCondition.getServeType().getId(),
                "id", "NE", visitCondition.getId()
        )).orElse(null) != null) throw new RuntimeException("当前服务类型已定义条件！");


        return visitConditionRepository.saveAndFlush(visitCondition);
    }

    /***
     * 删除
     */
    public String delete(SimpleRequest request) {
        isValid(request, visitConditionRepository);
        VisitCondition visitCondition = request.getO(VisitCondition.class);
        visitConditionRepository.delete(visitCondition);
        return "删除成功！";
    }

    /***
     * 数据是否合法
     * @param request
     * @param repository
     */
    private Model isValid(SimpleRequest request, CustomRepository repository) {
        Model oldModel = (Model) repository.findOne((String) request.get("id"));
        if (oldModel == null) throw new RuntimeException("编码【" + request.get("id") + "】在系统中不存在。");
        if (oldModel.getVersion() != (int) request.get("version")) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return oldModel;
    }
}
